package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.entity.guang.CarListEntity
import com.c.ctgproject.ui.collect.*
import com.c.ctgproject.utils.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource

//type 1:动力电池 散件录入 我的录入 2 电池搜索
class BatteryInWarehouseAdapter(private val context: Context, private val mList: List<BatteryInWarehouseEntity.DataBean.ListBean>, private var hasBtn: Boolean = true, private val type: Int? = null,private val mViewModel:PowerBatteryInWarehouseViewModel?) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_battery_in_warehouse_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mBottomRL.visibility = if (hasBtn) View.VISIBLE else View.GONE
            if(mViewModel!=null){
                if(mViewModel?.searchMine?.value==true){
                    holder.mCheckBox.visibility=View.GONE
                }else{
                    holder.mCheckBox.visibility=View.VISIBLE
                }
            }
            holder.mCheckBox.isChecked = data.checked
            holder.mCheckBox.setOnClickListener {
                if(holder.mCheckBox.isChecked){
                    mViewModel?.addCarId(data.orderId)
                }else{
                    mViewModel?.removeCarId(data.orderId)
                }
//                mList[position].checked = holder.mCheckBox.isChecked
//                cListener.onWeightChecked(position,holder.mCheckBox.isChecked)
//                notifyItemChanged(position)
            }
            if (hasBtn) {
                holder.mBTN.visibility=View.GONE
                holder.mBottomRL.visibility = View.VISIBLE
                holder.mLine.visibility = View.VISIBLE
//                when (data.status) {
//                    0 -> {
//                        holder.mBottomRL.visibility = View.VISIBLE
//                        holder.mLine.visibility = View.VISIBLE
//
////                    holder.mStatueTV.text="待入库"
////                    holder.mStatueTV.textColorResource=R.color.red
//                    }
//                    1 -> {
//                        holder.mBottomRL.visibility = View.GONE
//                        holder.mLine.visibility = View.GONE
////                    holder.mStatueTV.text="已入库"
////                    holder.mStatueTV.textColorResource=R.color.colorAccent
//
//                    }
//                    else -> {
////                    holder.mStatueTV.text=""
//                    }
//                }
            } else {
                if (data.isEditable == 1 && type == 1) {
                    holder.mBTN.text = "编辑"
                    holder.mBottomRL.visibility = View.VISIBLE
                    holder.mLine.visibility = View.VISIBLE
                } else {
                    holder.mBottomRL.visibility = View.GONE
                    holder.mLine.visibility = View.GONE
                }
            }
            if(mViewModel?.searchMine?.value==true){
                holder.mBTN.text = "编辑"
                holder.mBottomRL.visibility = View.VISIBLE
                holder.mLine.visibility = View.VISIBLE
            }



            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            holder.batteryCode.text = data.batteryCode.noDataHandler()
//            holder.carPlate.text = data.carPlate.noDataHandler()
            holder.batterySelfCode.text = data.batterySelfCode.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mCodeTV.text = data.orderNo.noDataHandler()
            holder.mBatteryNameTV.text = data.batteryName.noDataHandler()
            if(type==2){
                holder.mCodeTV.text=data.batterySelfCode.noDataHandler()
            }
            val batteryType = when (data.batteryType) {
                1 -> "电池包"
                2 -> "模组"
                3 -> "电芯"
                else -> "暂无数据"
            }
            holder.mBatteryModelTV.text = batteryType
            holder.mBatteryCountTV.text = data.count.noDataHandler()
            holder.mBatteryFromTV.text = data.batterySource.noDataHandler()
            holder.carCode.text=data.carCode.handlerNull()
//
//
            holder.mBottomRL.setOnClickListener {
                holder.mBTN.performClick()
            }
            holder.mBTN.setOnClickListener {
                if (type==1&&data.isEditable==1){
                    //动力电池-散件录入-我的录入-重新编辑录入
                    context.startActivity<PowerBatteryInWarehouseSingleActivity>(MyConstants.ORDER_ID to data.orderId)

                }
                else{
                    //过磅
                    if (data.orderId == null) {
                        context.showToast("该电池暂时无法进行过磅")
                    } else {
                        val mOrderIdList=ArrayList<Int>()
                        mOrderIdList.add(data.orderId)
                        context.startActivity<BatteryGetWeightActivity>(
                            MyConstants.POSITION to position,

                            MyConstants.ORDER_ID to data.orderId,
                            MyConstants.ORDER_ID_LIST to getGson().toJson(mOrderIdList)
                        )
                    }
                }
            }
            //详情
            holder.mParentLL.setOnClickListener {
                if (data.orderId == null) {
                    context.showToast("该电池暂无详情")
                } else if(type==2){

                    context.startActivity<BatteryCanHandlerDetailActivity>(MyConstants.ID to data.pbId)
                }else {
                    //入库单详情
                    context.startActivity<BatteryDetailActivity>(MyConstants.ORDER_ID to data.orderId)

                }

            }

        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        //        val carPlate: TextView = view.findViewById(R.id.carPlate)
        val batteryCode: TextView = view.findViewById(R.id.batteryCode)
        val mCodeTV: TextView = view.findViewById(R.id.mCodeTV)
        val batterySelfCode: TextView = view.findViewById(R.id.batterySelfCode)
        val mLine: View = view.findViewById(R.id.mLine)
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)

        //        val mStatueTV: TextView = view.findViewById(R.id.mStatueTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mBatteryNameTV: TextView = view.findViewById(R.id.mBatteryNameTV)
        val mBatteryModelTV: TextView = view.findViewById(R.id.mBatteryModelTV)
        val mBatteryCountTV: TextView = view.findViewById(R.id.mBatteryCountTV)
        val mBatteryFromTV: TextView = view.findViewById(R.id.mBatteryFromTV)
        val mBottomRL: RelativeLayout = view.findViewById(R.id.mBottomRL)
        val mBTN: Button = view.findViewById(R.id.mBTN)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mCheckBox: CheckBox =view.findViewById(R.id.mCheckBox)
        val carCode:TextView=view.findViewById(R.id.carCode)
    }


}